using System;
using System.Collections;
using Cafechess.Chess.Validation;
using Cafechess.Chess.Parsers;
using Cafechess.Chess.Controls;
using Cafechess.Chess.General;

namespace FENImages
{
	/// <summary>
	/// Summary description for GenericValidation.
	/// </summary>
	public class GenericValidation : IValidation, IPositionEvents
	{
    
    delegate void UpdateBoard( Operation op, string FromSquare, string ToSquare,Pieces piece);
    event UpdateBoard EventUpdateBoard;
    ChessBoard coChessBoard;
    bool coSideToMove;
    public StandardValidation Validation{ get{ return coValidation;} }
    private StandardValidation coValidation;
    private FenNotation coFen;

		public GenericValidation(ChessBoard chessboard)
		{
      coChessBoard = chessboard;
      coSideToMove=true;
      coFen = new FenNotation();
      coValidation = new StandardValidation();
      coFen.AddEvents(coValidation);
      

    }

    #region IValidation Members

    public bool Move(Pieces piece, string fromSquare, string toSquare)
    {

      if( EventUpdateBoard != null )
      {
          EventUpdateBoard(Operation.MOVE, fromSquare, toSquare, Pieces.None);
        coChessBoard.ChessBoardGui.IsRedraw = true;
        coChessBoard.Invalidate();
      }
      return true;
    }

    bool IValidation.Move(string algebraic)
    {
      // TODO:  Add EditPosition.Cafechess.Validation.IValidation.Move implementation
      return true;
    }

    public void AddEvents(IValidationEvents ievents)
    {

    }

    public void RemoveEvents(IValidationEvents ievents)
    {
    }

    void IValidation.AddEvents(IValidationUIEvents ievents)
    {
      EventUpdateBoard += new UpdateBoard(ievents.UpdateBoard);
    }

    void IValidation.RemoveEvents(IValidationUIEvents ievents)
    {
      EventUpdateBoard -= new UpdateBoard(ievents.UpdateBoard);
    }

    public IPositionEvents PositionEvents
    {
      get
      {
        // TODO:  Add EditPosition.PositionEvents getter implementation
        return this;
      }
    }

    public string Notation
    {
      get
      {
        // TODO:  Add EditPosition.Notation getter implementation
        return null;
      }
    }

    public string FenNotation
    {
      get
      {        
        return ToString();
      }
      set
      {
        coChessBoard.ChessBoardGui.FenNotation = value;
      }
    }
    public string FenFormat{set{/*coFenNotation.Format = value;*/ }}

    public string FullNotation
    {
      get
      {
        return  null;
      }
    }

    public string MovedTo
    {
      get
      {
        // TODO:  Add EditPosition.MovedTo getter implementation
        return null;
      }
      set
      {
        // TODO:  Add EditPosition.MovedTo setter implementation
      }
    }

    public string MovedFrom
    {
      get
      {
        // TODO:  Add EditPosition.MovedFrom getter implementation
        return null;
      }
      set
      {
        // TODO:  Add EditPosition.MovedFrom setter implementation
      }
    }

    public bool SideToMove
    {
      get
      {
        return coSideToMove;
      }
      set
      {
        coValidation.SideToMove = value;
        coSideToMove = value;
      }
    }

    public bool IsValidMove
    {
      get
      {
        // TODO:  Add EditPosition.IsValidMove getter implementation
        return true;
      }
      set
      {
        // TODO:  Add EditPosition.IsValidMove setter implementation
      }
    }

    public bool IsValidating
    {
      get
      {
        // TODO:  Add EditPosition.IsValidating getter implementation
        return true;
      }
      set
      {
        // TODO:  Add EditPosition.IsValidating setter implementation
      }
    }

    public BitArray Attacks(bool sideToMove)
    {
      coFen.Parse(this.coChessBoard.ChessBoardGui.ImageFenNotation);
      return coValidation.Attacks(sideToMove);
    }

    public BitArray Moves(Pieces piece, int square)
    {
      coFen.Parse(this.coChessBoard.ChessBoardGui.ImageFenNotation);
      return coValidation.Moves( piece, square);    
    }

    public int[] WeightWeakSquares() { return coValidation.WeightWeakSquares(); }

    #endregion

    #region IPositionEvents Members

    public void PlacePiece(Pieces piece, int square)
    {
    }

    public void SetSideToMove(bool bColor)
    {
    }

    public void SetCastling(bool WK, bool WQ, bool BK, bool BQ)
    {
    }

    public void SetEnpassant(string Enpassant)
    {
    }

    public void SetHalfMoves(int number)
    {
    }

    public void SetFullMoves(int number)
    {
    }

    public void Finished()
    {
    }

    public void Starting()
    {
      // TODO:  Add EditPosition.starting implementation
    }

    #endregion

  }
}
